﻿namespace REVISIONMAX30
{
    using MySql.Data.MySqlClient;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;

    public class MySQLItemList : UserControl
    {

        private ToolStripButton btnDelete;
        private ToolStripButton btnEdit;
        private ToolStripButton btnExecute;
        private ToolStripButton btnExecuteViaADODB;
        private ToolStripButton btnExpire;
        private ToolStripButton btnNew;
        private ToolStripButton btnRefresh;
        private ToolStripButton btnReset;
        private ToolStripButton btnSetConnectionString;
        private ToolStripButton btnSetRevisionAndIndex;
        private ToolStripButton btnShowExpired;
        private ToolStripButton btnView;
        private ToolStripComboBox cboConnectionStrings;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private ColumnHeader columnHeader3;
        private ColumnHeader columnHeader4;
        private ColumnHeader columnHeader5;
        private ColumnHeader columnHeader6;
        private ColumnHeader columnHeader7;
        private ColumnHeader columnHeader8;
        private IContainer components = null;
        private Controls.MyListView lvSQLItems;
        private ToolStrip toolStrip1;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripSeparator toolStripSeparator10;
        private ToolStripSeparator toolStripSeparator11;
        private ToolStripSeparator toolStripSeparator2;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStripSeparator toolStripSeparator4;
        private ToolStripSeparator toolStripSeparator5;
        private ToolStripSeparator toolStripSeparator6;
        private ToolStripSeparator toolStripSeparator7;
        private ToolStripSeparator toolStripSeparator8;
        private ToolStripSeparator toolStripSeparator9;
        private ToolStripTextBox txtIndex;
        private ToolStripTextBox txtRevision;

        public MySQLItemList()
        {
            this.InitializeComponent();
        }


        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.btnNew = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.btnEdit = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.btnDelete = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.btnRefresh = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            this.btnExecute = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator11 = new System.Windows.Forms.ToolStripSeparator();
            this.btnExecuteViaADODB = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator6 = new System.Windows.Forms.ToolStripSeparator();
            this.btnReset = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator7 = new System.Windows.Forms.ToolStripSeparator();
            this.btnShowExpired = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator8 = new System.Windows.Forms.ToolStripSeparator();
            this.btnExpire = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator();
            this.btnView = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator9 = new System.Windows.Forms.ToolStripSeparator();
            this.btnSetConnectionString = new System.Windows.Forms.ToolStripButton();
            this.cboConnectionStrings = new System.Windows.Forms.ToolStripComboBox();
            this.toolStripSeparator10 = new System.Windows.Forms.ToolStripSeparator();
            this.btnSetRevisionAndIndex = new System.Windows.Forms.ToolStripButton();
            this.txtRevision = new System.Windows.Forms.ToolStripTextBox();
            this.txtIndex = new System.Windows.Forms.ToolStripTextBox();
            this.lvSQLItems = new REVISIONMAX30.Controls.MyListView(this.components);
            this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader8 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader5 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader6 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader7 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.toolStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // toolStrip1
            // 
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnNew,
            this.toolStripSeparator1,
            this.btnEdit,
            this.toolStripSeparator2,
            this.btnDelete,
            this.toolStripSeparator3,
            this.btnRefresh,
            this.toolStripSeparator4,
            this.btnExecute,
            this.toolStripSeparator11,
            this.btnExecuteViaADODB,
            this.toolStripSeparator6,
            this.btnReset,
            this.toolStripSeparator7,
            this.btnShowExpired,
            this.toolStripSeparator8,
            this.btnExpire,
            this.toolStripSeparator5,
            this.btnView,
            this.toolStripSeparator9,
            this.btnSetConnectionString,
            this.cboConnectionStrings,
            this.toolStripSeparator10,
            this.btnSetRevisionAndIndex,
            this.txtRevision,
            this.txtIndex});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(1073, 25);
            this.toolStrip1.TabIndex = 15;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // btnNew
            // 
            this.btnNew.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnNew.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnNew.Name = "btnNew";
            this.btnNew.Size = new System.Drawing.Size(32, 22);
            this.btnNew.Text = "New";
            this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // btnEdit
            // 
            this.btnEdit.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnEdit.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnEdit.Name = "btnEdit";
            this.btnEdit.Size = new System.Drawing.Size(29, 22);
            this.btnEdit.Text = "Edit";
            this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
            // 
            // btnDelete
            // 
            this.btnDelete.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnDelete.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new System.Drawing.Size(86, 22);
            this.btnDelete.Text = "Delete Selected";
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(6, 25);
            // 
            // btnRefresh
            // 
            this.btnRefresh.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnRefresh.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnRefresh.Name = "btnRefresh";
            this.btnRefresh.Size = new System.Drawing.Size(49, 22);
            this.btnRefresh.Text = "Refresh";
            this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
            // 
            // toolStripSeparator4
            // 
            this.toolStripSeparator4.Name = "toolStripSeparator4";
            this.toolStripSeparator4.Size = new System.Drawing.Size(6, 25);
            // 
            // btnExecute
            // 
            this.btnExecute.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnExecute.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnExecute.Name = "btnExecute";
            this.btnExecute.Size = new System.Drawing.Size(135, 22);
            this.btnExecute.Text = "Execute MySQL 5.1 Batch";
            this.btnExecute.Click += new System.EventHandler(this.btnExecute_Click);
            // 
            // toolStripSeparator11
            // 
            this.toolStripSeparator11.Name = "toolStripSeparator11";
            this.toolStripSeparator11.Size = new System.Drawing.Size(6, 25);
            // 
            // btnExecuteViaADODB
            // 
            this.btnExecuteViaADODB.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnExecuteViaADODB.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnExecuteViaADODB.Name = "btnExecuteViaADODB";
            this.btnExecuteViaADODB.Size = new System.Drawing.Size(105, 22);
            this.btnExecuteViaADODB.Text = "Execute Via ADODB";
            this.btnExecuteViaADODB.Click += new System.EventHandler(this.btnExecuteViaADODB_Click);
            // 
            // toolStripSeparator6
            // 
            this.toolStripSeparator6.Name = "toolStripSeparator6";
            this.toolStripSeparator6.Size = new System.Drawing.Size(6, 25);
            // 
            // btnReset
            // 
            this.btnReset.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnReset.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnReset.Name = "btnReset";
            this.btnReset.Size = new System.Drawing.Size(39, 22);
            this.btnReset.Text = "Reset";
            this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
            // 
            // toolStripSeparator7
            // 
            this.toolStripSeparator7.Name = "toolStripSeparator7";
            this.toolStripSeparator7.Size = new System.Drawing.Size(6, 25);
            // 
            // btnShowExpired
            // 
            this.btnShowExpired.CheckOnClick = true;
            this.btnShowExpired.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnShowExpired.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnShowExpired.Name = "btnShowExpired";
            this.btnShowExpired.Size = new System.Drawing.Size(76, 22);
            this.btnShowExpired.Text = "Show Expired";
            this.btnShowExpired.Click += new System.EventHandler(this.btnShowExpired_Click);
            // 
            // toolStripSeparator8
            // 
            this.toolStripSeparator8.Name = "toolStripSeparator8";
            this.toolStripSeparator8.Size = new System.Drawing.Size(6, 25);
            // 
            // btnExpire
            // 
            this.btnExpire.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnExpire.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnExpire.Name = "btnExpire";
            this.btnExpire.Size = new System.Drawing.Size(41, 22);
            this.btnExpire.Text = "Expire";
            this.btnExpire.Click += new System.EventHandler(this.btnExpire_Click);
            // 
            // toolStripSeparator5
            // 
            this.toolStripSeparator5.Name = "toolStripSeparator5";
            this.toolStripSeparator5.Size = new System.Drawing.Size(6, 25);
            // 
            // btnView
            // 
            this.btnView.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnView.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnView.Name = "btnView";
            this.btnView.Size = new System.Drawing.Size(33, 22);
            this.btnView.Text = "View";
            this.btnView.Click += new System.EventHandler(this.btnView_Click);
            // 
            // toolStripSeparator9
            // 
            this.toolStripSeparator9.Name = "toolStripSeparator9";
            this.toolStripSeparator9.Size = new System.Drawing.Size(6, 25);
            // 
            // btnSetConnectionString
            // 
            this.btnSetConnectionString.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnSetConnectionString.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSetConnectionString.Name = "btnSetConnectionString";
            this.btnSetConnectionString.Size = new System.Drawing.Size(74, 22);
            this.btnSetConnectionString.Text = "Set Con. Str.";
            this.btnSetConnectionString.Click += new System.EventHandler(this.btnSetConnectionString_Click);
            // 
            // cboConnectionStrings
            // 
            this.cboConnectionStrings.Name = "cboConnectionStrings";
            this.cboConnectionStrings.Size = new System.Drawing.Size(121, 25);
            // 
            // toolStripSeparator10
            // 
            this.toolStripSeparator10.Name = "toolStripSeparator10";
            this.toolStripSeparator10.Size = new System.Drawing.Size(6, 25);
            // 
            // btnSetRevisionAndIndex
            // 
            this.btnSetRevisionAndIndex.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnSetRevisionAndIndex.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSetRevisionAndIndex.Name = "btnSetRevisionAndIndex";
            this.btnSetRevisionAndIndex.Size = new System.Drawing.Size(87, 22);
            this.btnSetRevisionAndIndex.Text = "Set Rev / Index";
            this.btnSetRevisionAndIndex.Click += new System.EventHandler(this.btnSetRevisionAndIndex_Click);
            // 
            // txtRevision
            // 
            this.txtRevision.Name = "txtRevision";
            this.txtRevision.Size = new System.Drawing.Size(100, 21);
            // 
            // txtIndex
            // 
            this.txtIndex.Name = "txtIndex";
            this.txtIndex.Size = new System.Drawing.Size(100, 21);
            // 
            // lvSQLItems
            // 
            this.lvSQLItems.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lvSQLItems.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2,
            this.columnHeader3,
            this.columnHeader4,
            this.columnHeader8,
            this.columnHeader5,
            this.columnHeader6,
            this.columnHeader7});
            this.lvSQLItems.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lvSQLItems.FullRowSelect = true;
            this.lvSQLItems.GridLines = true;
            this.lvSQLItems.HideSelection = false;
            this.lvSQLItems.Location = new System.Drawing.Point(0, 25);
            this.lvSQLItems.Name = "lvSQLItems";
            this.lvSQLItems.Size = new System.Drawing.Size(1073, 203);
            this.lvSQLItems.TabIndex = 16;
            this.lvSQLItems.UseCompatibleStateImageBehavior = false;
            this.lvSQLItems.View = System.Windows.Forms.View.Details;
            this.lvSQLItems.DoubleClick += new System.EventHandler(this.lvSQLItems_DoubleClick);
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "Index";
            this.columnHeader1.Width = 38;
            // 
            // columnHeader2
            // 
            this.columnHeader2.Text = "Source";
            this.columnHeader2.Width = 79;
            // 
            // columnHeader3
            // 
            this.columnHeader3.Text = "Type";
            this.columnHeader3.Width = 79;
            // 
            // columnHeader4
            // 
            this.columnHeader4.Text = "Revision";
            this.columnHeader4.Width = 63;
            // 
            // columnHeader8
            // 
            this.columnHeader8.Text = "con Str";
            // 
            // columnHeader5
            // 
            this.columnHeader5.Text = "Created";
            this.columnHeader5.Width = 83;
            // 
            // columnHeader6
            // 
            this.columnHeader6.Text = "Updated";
            this.columnHeader6.Width = 89;
            // 
            // columnHeader7
            // 
            this.columnHeader7.Text = "Executed";
            this.columnHeader7.Width = 87;
            // 
            // MySQLItemList
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.lvSQLItems);
            this.Controls.Add(this.toolStrip1);
            this.Name = "MySQLItemList";
            this.Size = new System.Drawing.Size(1073, 228);
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        //private ObjectBase _parent;
        //private UIHandler _uiHandler = null;

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //List<int> idArray = new List<int>();
            //foreach (ListViewItem item in this.lvSQLItems.SelectedItems)
            //{
            //    idArray.Add((int)item.Tag);
            //}
            //if (idArray.Count > 0)
            //{
            //    Common.DeleteSQLItems(idArray);
            //}
            //this.UpdateViewer();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.Edit();
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            //StringBuilder builder = new StringBuilder();
            //foreach (ListViewItem item in this.lvSQLItems.SelectedItems)
            //{
            //    SortedList<int, string> connectionStringIdValueList = Common.GetConnectionStringIdValueList(this._parent);
            //    try
            //    {
            //        SQLItem sQLItemById = Common.GetSQLItemById((int)item.Tag, this._parent);
            //        if (connectionStringIdValueList.ContainsKey(sQLItemById.connectionstringid))
            //        {
            //            string connectionString = connectionStringIdValueList[sQLItemById.connectionstringid];
            //            MySqlConnectionStringBuilder builder2 = new MySqlConnectionStringBuilder(connectionString);
            //            using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //            {
            //                string str2;
            //                connection.Open();
            //                REVISIONMAX30.Log pErrorLog = new REVISIONMAX30.Log("", this._parent, connection);
            //                REVISIONMAX30.Log pOutLog = new REVISIONMAX30.Log("", this._parent, connection);
            //                if (sQLItemById.sourcetype == "sqlfile")
            //                {
            //                    str2 = Common.ExecuteBatch(builder2.UserID, builder2.Password, builder2.Database, sQLItemById.source, pErrorLog, pOutLog);
            //                    if (!str2.IsEmpty())
            //                    {
            //                        builder.AppendLine(str2);
            //                    }
            //                    if (builder.Length > 0)
            //                    {
            //                        break;
            //                    }
            //                    sQLItemById.executed = new DateTime?(DateTime.Now);
            //                    builder.Append(sQLItemById.SaveOrUpdate().ToString());
            //                    if (builder.Length > 0)
            //                    {
            //                        break;
            //                    }
            //                }
            //                else if (sQLItemById.sourcetype == "sourcetext")
            //                {
            //                    string tempFileName = Path.GetTempFileName();
            //                    File.WriteAllText(tempFileName, sQLItemById.source);
            //                    str2 = Common.ExecuteBatch(builder2.UserID, builder2.Password, builder2.Database, tempFileName, pErrorLog, pOutLog);
            //                    File.Delete(tempFileName);
            //                    if (!str2.IsEmpty())
            //                    {
            //                        builder.AppendLine(str2);
            //                    }
            //                    if (builder.Length > 0)
            //                    {
            //                        break;
            //                    }
            //                    sQLItemById.executed = new DateTime?(DateTime.Now);
            //                    builder.Append(sQLItemById.SaveOrUpdate().ToString());
            //                    if (builder.Length > 0)
            //                    {
            //                        break;
            //                    }
            //                }
            //                if (!pErrorLog.value.IsEmpty())
            //                {
            //                    pErrorLog.SaveOrUpdate();
            //                }
            //                if (!pOutLog.value.IsEmpty())
            //                {
            //                    pOutLog.SaveOrUpdate();
            //                }
            //            }
            //        }
            //    }
            //    catch (Exception exception)
            //    {
            //        builder.AppendLine(string.Format("Error: {0}", exception.Message));
            //    }
            //    if (builder.Length > 0)
            //    {
            //        MessageBox.Show(builder.ToString());
            //    }
            //}
            //this.UpdateViewer();
        }

        private void btnExecuteViaADODB_Click(object sender, EventArgs e)
        {
            //StringBuilder builder = new StringBuilder();
            //foreach (ListViewItem item in this.lvSQLItems.SelectedItems)
            //{
            //    SortedList<int, string> connectionStringIdValueList = Common.GetConnectionStringIdValueList(this._parent);
            //    try
            //    {
            //        SQLItem sQLItemById = Common.GetSQLItemById((int)item.Tag, this._parent);
            //        if (connectionStringIdValueList.ContainsKey(sQLItemById.connectionstringid))
            //        {
            //            string connectionString = connectionStringIdValueList[sQLItemById.connectionstringid];
            //            MySqlConnectionStringBuilder builder2 = new MySqlConnectionStringBuilder(connectionString);
            //            using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //            {
            //                string str2;
            //                connection.Open();
            //                REVISIONMAX30.Log pErrorLog = new REVISIONMAX30.Log("", this._parent, connection);
            //                REVISIONMAX30.Log pOutLog = new REVISIONMAX30.Log("", this._parent, connection);
            //                if (sQLItemById.sourcetype == "sqlfile")
            //                {
            //                    str2 = Common.ExecuteViaMYSQL(sQLItemById.source, builder2.ConnectionString, pErrorLog, pOutLog);
            //                    if (!str2.IsEmpty())
            //                    {
            //                        builder.AppendLine(str2);
            //                    }
            //                    if (builder.Length > 0)
            //                    {
            //                        break;
            //                    }
            //                    sQLItemById.executed = new DateTime?(DateTime.Now);
            //                    builder.Append(sQLItemById.SaveOrUpdate().ToString());
            //                    if (builder.Length > 0)
            //                    {
            //                        break;
            //                    }
            //                }
            //                else if (sQLItemById.sourcetype == "sourcetext")
            //                {
            //                    string tempFileName = Path.GetTempFileName();
            //                    File.WriteAllText(tempFileName, sQLItemById.source);
            //                    str2 = Common.ExecuteViaMYSQL(tempFileName, builder2.ConnectionString, pErrorLog, pOutLog);
            //                    File.Delete(tempFileName);
            //                    if (!str2.IsEmpty())
            //                    {
            //                        builder.AppendLine(str2);
            //                    }
            //                    if (builder.Length > 0)
            //                    {
            //                        break;
            //                    }
            //                    sQLItemById.executed = new DateTime?(DateTime.Now);
            //                    builder.Append(sQLItemById.SaveOrUpdate().ToString());
            //                    if (builder.Length > 0)
            //                    {
            //                        break;
            //                    }
            //                }
            //                if (!pErrorLog.value.IsEmpty())
            //                {
            //                    pErrorLog.SaveOrUpdate();
            //                }
            //                if (!pOutLog.value.IsEmpty())
            //                {
            //                    pOutLog.SaveOrUpdate();
            //                }
            //            }
            //        }
            //    }
            //    catch (Exception exception)
            //    {
            //        builder.AppendLine(string.Format("Error: {0}", exception.Message));
            //    }
            //    if (builder.Length > 0)
            //    {
            //        MessageBox.Show(builder.ToString());
            //    }
            //}
            //this.UpdateViewer();
        }

        private void btnExpire_Click(object sender, EventArgs e)
        {
            //StringBuilder errors = new StringBuilder();
            //try
            //{
            //    using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //    {
            //        connection.Open();
            //        using (SQLiteTransaction transaction = connection.BeginTransaction())
            //        {
            //            using (SQLiteCommand command = connection.CreateSQLItemCommand())
            //            {
            //                foreach (ListViewItem item in this.lvSQLItems.SelectedItems)
            //                {
            //                    SQLItem sQLItemById = Common.GetSQLItemById((int)item.Tag, this._parent);
            //                    DateTime? expired = sQLItemById.expired;
            //                    if (!expired.HasValue)
            //                    {
            //                        sQLItemById.expired = new DateTime?(DateTime.Now);
            //                    }
            //                    else
            //                    {
            //                        sQLItemById.expired = null;
            //                    }
            //                    sQLItemById.SaveOrUpdate(command, errors);
            //                }
            //                transaction.Commit();
            //            }
            //        }
            //    }
            //}
            //catch (Exception exception)
            //{
            //    errors.AppendLine(string.Format("Error: {0}", exception.Message));
            //}
            //if (errors.Length > 0)
            //{
            //    MessageBox.Show(errors.ToString());
            //}
            //this.UpdateViewer();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            //using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //{
            //    connection.Open();
            //    this._uiHandler.ViewSQLItem(new SQLItem("", "", this._parent, connection));
            //}
            //this.UpdateViewer();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.UpdateViewer();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            //StringBuilder errors = new StringBuilder();
            //try
            //{
            //    using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //    {
            //        connection.Open();
            //        using (SQLiteTransaction transaction = connection.BeginTransaction())
            //        {
            //            using (SQLiteCommand command = connection.CreateSQLItemCommand())
            //            {
            //                foreach (ListViewItem item in this.lvSQLItems.SelectedItems)
            //                {
            //                    SQLItem sQLItemById = Common.GetSQLItemById((int)item.Tag, this._parent);
            //                    sQLItemById.executed = null;
            //                    sQLItemById.SaveOrUpdate(command, errors);
            //                }
            //                transaction.Commit();
            //            }
            //        }
            //    }
            //}
            //catch (Exception exception)
            //{
            //    errors.AppendLine(string.Format("Error: {0}", exception.Message));
            //}
            //if (errors.Length > 0)
            //{
            //    MessageBox.Show(errors.ToString());
            //}
            //this.UpdateViewer();
        }

        private void btnSetConnectionString_Click(object sender, EventArgs e)
        {
            //if (this.lvSQLItems.SelectedItems.Count != 0)
            //{
            //    string selectedText = this.cboConnectionStrings.SelectedText;
            //    SortedList<string, int> connectionStringNameIdList = Common.GetConnectionStringNameIdList(this._parent);
            //    if (!(!selectedText.IsEmpty() && connectionStringNameIdList.ContainsKey(selectedText)))
            //    {
            //        MessageBox.Show("connectionstring no specified for selection.");
            //    }
            //    else
            //    {
            //        StringBuilder errors = new StringBuilder();
            //        try
            //        {
            //            using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //            {
            //                connection.Open();
            //                using (SQLiteTransaction transaction = connection.BeginTransaction())
            //                {
            //                    using (SQLiteCommand command = connection.CreateSQLItemCommand())
            //                    {
            //                        foreach (ListViewItem item in this.lvSQLItems.SelectedItems)
            //                        {
            //                            SQLItem sQLItemById = Common.GetSQLItemById((int)item.Tag, this._parent);
            //                            sQLItemById.connectionstringid = connectionStringNameIdList[selectedText];
            //                            sQLItemById.SaveOrUpdate(command, errors);
            //                        }
            //                        transaction.Commit();
            //                    }
            //                }
            //            }
            //        }
            //        catch (Exception exception)
            //        {
            //            errors.AppendLine(string.Format("Error: {0}", exception.Message));
            //        }
            //        if (errors.Length > 0)
            //        {
            //            MessageBox.Show(errors.ToString());
            //        }
            //        this.UpdateViewer();
            //    }
            //}
        }

        private void btnSetRevisionAndIndex_Click(object sender, EventArgs e)
        {
            //if (this.lvSQLItems.SelectedItems.Count != 0)
            //{
            //    string text = this.txtRevision.Text;
            //    int num = this.txtIndex.Text.IntValueOrZero();
            //    if ((num == 0) || text.IsEmpty())
            //    {
            //        MessageBox.Show("revision must be specified and index must be more then 0.");
            //    }
            //    else
            //    {
            //        StringBuilder errors = new StringBuilder();
            //        try
            //        {
            //            using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //            {
            //                connection.Open();
            //                using (SQLiteTransaction transaction = connection.BeginTransaction())
            //                {
            //                    using (SQLiteCommand command = connection.CreateSQLItemCommand())
            //                    {
            //                        foreach (ListViewItem item in this.lvSQLItems.SelectedItems)
            //                        {
            //                            SQLItem sQLItemById = Common.GetSQLItemById((int)item.Tag, this._parent);
            //                            sQLItemById.revision = text.IntValueOrZero();
            //                            sQLItemById.index = num;
            //                            sQLItemById.SaveOrUpdate(command, errors);
            //                        }
            //                        transaction.Commit();
            //                    }
            //                }
            //            }
            //        }
            //        catch (Exception exception)
            //        {
            //            errors.AppendLine(string.Format("Error: {0}", exception.Message));
            //        }
            //        if (errors.Length > 0)
            //        {
            //            MessageBox.Show(errors.ToString());
            //        }
            //        this.UpdateViewer();
            //    }
            //}
        }

        private void btnShowExpired_Click(object sender, EventArgs e)
        {
            this.UpdateViewer();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            //foreach (ListViewItem item in this.lvSQLItems.SelectedItems)
            //{
            //    SQLItem sQLItemById = Common.GetSQLItemById((int)item.Tag, this._parent);
            //    if (sQLItemById.sourcetype == "sqlfile")
            //    {
            //        Process.Start(sQLItemById.source);
            //    }
            //}
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void Edit()
        {
            //if (this.lvSQLItems.SelectedItems.Count > 0)
            //{
            //    this.uiHandler.ViewSQLItem(Common.GetSQLItemById((int)this.lvSQLItems.SelectedItems[0].Tag, this._parent));
            //}
            //this.UpdateViewer();
        }


        //public void Initzialize(UIHandler puiHandler, ObjectBase parent)
        //{
        //    this._uiHandler = puiHandler;
        //    this._parent = parent;
        //    this.UpdateViewer();
        //}

        private void lvSQLItems_DoubleClick(object sender, EventArgs e)
        {
            this.Edit();
        }

        public void UpdateViewer()
        {
            //this.cboConnectionStrings.Items.Clear();
            //foreach (DataRow row in Common.GetConnectionStringDataTable(this._parent.id).Rows)
            //{
            //    this.cboConnectionStrings.Items.Add(row["name"].StringValueOrEmpty());
            //}
            //this.lvSQLItems.Items.Clear();
            //SortedList<int, string> connectionStringIdNameList = Common.GetConnectionStringIdNameList(this._parent);
            //using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //{
            //    connection.Open();
            //    using (SQLiteCommand command = connection.CreateCommand())
            //    {
            //        command.CommandText = string.Format("select * from sqlitems  where  `parentid` = {0} {1} order by `connectionstringid`,  cast(`revision` as int), `index`", this._parent.id, !this.btnShowExpired.Checked ? " AND `expired` is null " : "");
            //        SQLiteDataReader reader = command.ExecuteReader();
            //        while (reader.Read())
            //        {
            //            SQLItem item = new SQLItem(reader, this._parent);
            //            ListViewItem item2 = this.lvSQLItems.Items.Add(item.index.ToString());
            //            item2.Tag = item.id;

            //            if (item.sourcetype == "sqlfile")
            //            {
            //                if (File.Exists(item.source))
            //                {
            //                    item2.SubItems.Add(Path.GetFileNameWithoutExtension(item.source));
            //                }
            //                else
            //                {
            //                    item2.SubItems.Add(item.source);
            //                }
            //            }
            //            else
            //            {
            //                item2.SubItems.Add("[sql text]" + item.source);
            //            }

            //            item2.SubItems.Add(item.sourcetype);
            //            item2.SubItems.Add(item.revision.StringValueOrEmpty());
            //            if (connectionStringIdNameList.ContainsKey(item.connectionstringid))
            //            {
            //                item2.SubItems.Add(connectionStringIdNameList[item.connectionstringid]);
            //            }
            //            else
            //            {
            //                item2.SubItems.Add("");
            //            }
            //            item2.SubItems.Add(item.created.StringValueOrEmpty());
            //            item2.SubItems.Add(item.updated.StringValueOrEmpty());
            //            item2.SubItems.Add(item.executed.StringValueOrEmpty());
            //            if (item.expired.HasValue)
            //            {
            //                item2.ForeColor = Color.Red;
            //            }
            //            else if (item.executed.HasValue)
            //            {
            //                item2.ForeColor = Color.DarkGreen;
            //            }
            //            else
            //            {
            //                item2.ForeColor = Color.DarkOrange;
            //            }
            //        }
            //    }
            //}
            //if (this.lvSQLItems.Items.Count > 0)
            //{
            //    foreach (ColumnHeader header in this.lvSQLItems.Columns)
            //    {
            //        header.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            //    }
            //}
        }

        //public UIHandler uiHandler
        //{
        //    get
        //    {
        //        return this._uiHandler;
        //    }
        //    set
        //    {
        //        this._uiHandler = value;
        //    }
        //}
    }
}

